const storage = {
  local: key => window.localStorage.getItem(key),
  session: key => window.sessionStorage.getItem(key),
  cookie: key => getCookie(key)
};

function getCookie(key) {
  var prefix = key + '=';
  var start = document.cookie.indexOf(prefix);

  if (start === -1) {
    return '';
  }

  var end = document.cookie.indexOf(';', start + prefix.length);
  if (end === -1) {
    end = document.cookie.length;
  }

  var value = document.cookie.substring(start + prefix.length, end);
  return unescape(value);
}

/**
 * @function 获取token
 * @param {string} key 键值
 * @param {string} position 存储位置 - local ｜ session｜ cookie
 */

export default (key, position) => {
  return storage[position] ? storage[position](key) : storage['cookie'](key);
};
